package com.charging.config.config;

import com.charging.bean.ApiDiscount;
import com.charging.bean.UserInfo;
import com.charging.config.result.ApiResult;
import com.charging.service.*;
import com.charging.util.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 张子艺
 * @packge com.controller.config
 * @data 2019-01-08 15:51
 * @project Currency
 */
@Configuration
public class BaseController {

    @Autowired
    protected UserInfoService userInfoService;
    @Autowired
    protected SiteService siteService;
    @Autowired
    protected SiteImageService siteImageService;
    @Autowired
    protected AdvertisementService advertisementService;
    @Autowired
    protected ApiDiscountService apiDiscountService;
    @Autowired
    protected UserService userService;





    public UserInfo getUserInfo(HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        return userInfo;
    }

    /***
     * 判断一个请求是否为AJAX请求,是则返回true
     * @param request
     * @return
     */
    public boolean isAjaxRequest(HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        //如果requestType能拿到值，并且值为 XMLHttpRequest ,表示客户端的请求为异步请求，那自然是ajax请求了，反之如果为null,则是普通的请求
        if (requestType == null) {
            return false;
        }
        return true;

    }

    /**
     * 获取当前请求
     *
     * @return response
     */
    protected HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    private void render(String text, String contentType){
        HttpServletResponse response;
        try{

            response = getResponse();
            response.setContentType(contentType);

            response.getWriter().write(text);
            response.getWriter().flush();
        }
        catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * 方法名称: renderJson<br>
     * 描述：返回JSON格式数据
     */
    protected void renderJson(String text){
        render(text,  MediaType.APPLICATION_JSON_UTF8_VALUE );
    }

    /**
     * 程序异常
     * 因为有切面日志的原因如果控制层报错 则会打印两次日志  一次是请求的切点 一次是 本异常类的切点
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult serverException(Exception e) {
        if (e instanceof CommonException) {
            return ApiResult.fail(e.getMessage());
        }
        e.printStackTrace(System.out);
        return ApiResult.getException();
    }






}
